/**
 * @Author: Sagan
 * @Date: 2020/09/17 16:55
 */
package module

import (
	"gitee.com/big_meteor/godash/lang"
	"gitee.com/big_meteor/godash/validate/constant"
	"reflect"
	"unicode/utf8"
)

// 是否是指定长度
// 支持的类型：string
func Length(v interface{}, length int) (bool, error) {
	var rt reflect.Kind
	v, rt = lang.GetValue(v)

	if reflect.String != rt {
		return false, constant.ERROR_LENGTH_UDT
	}

	l := utf8.RuneCountInString(v.(string))

	return length == l, nil
}

// 长度是否在某个区间
// 支持的类型：string
func LengthBetween(v interface{}, start, end int) (bool, error) {
	var rt reflect.Kind
	v, rt = lang.GetValue(v)

	if reflect.String != rt {
		return false, constant.ERROR_LENGTH_UDT
	}

	l := utf8.RuneCountInString(v.(string))

	return start <= l && l < end, nil
}
